﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinClock
{
    public static class ControlCloner
    {
        public static T CloneControl<T>(T controlToClone) where T : Control
        {
            PropertyInfo[] controlProperties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

            T clonedControl = Activator.CreateInstance<T>();

            foreach (PropertyInfo prop in controlProperties)
            {
                if (prop.CanWrite && prop.Name != "WindowTarget")
                {
                    try { prop.SetValue(clonedControl, prop.GetValue(controlToClone, null), null); }
                    catch { }
                }
            }
            foreach (Control control in controlToClone.Controls)
            {
                // 检查是否为Label类型
                if (control is Label)
                {
                    // 创建一个新的Label实例
                    Label newLabel = new Label();
                    // 设置新Label的属性，使其看起来和原来的一样
                    newLabel.Text = control.Text;
                    newLabel.Location = control.Location;
                    // 添加到destinationPanel中
                    clonedControl.Controls.Add(newLabel);
                }
            }
            return clonedControl;
        }
    }
}